package com.geostar.constructionland.analyse.service.handler.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.geostar.constructionland.common.dao.YzGeoJsonDao;
import com.geostar.constructionland.analyse.entity.HttpResult;
import com.geostar.constructionland.analyse.entity.alyresult.AlyResult;
import com.geostar.constructionland.analyse.entity.alyresult.AlyResultCdZd;
import com.geostar.constructionland.analyse.entity.model.AnalyseModel;
import com.geostar.constructionland.analyse.entity.resulthandler.HandlerResult;
import com.geostar.constructionland.analyse.service.analyseresult.CdZdService;
import com.geostar.constructionland.analyse.service.handler.IResultHandler;
import com.geostar.constructionland.analyse.service.handler.impl.util.XmlUtil;
import com.geostar.constructionland.common.entity.logs.mongolog.YzGeoJson;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 重叠宗地分析处理类
 */
@Component
@Slf4j
public class CdZdFxHandler implements IResultHandler {
    @Autowired
    ObjectMapper objectMapper;
    @Autowired
    CdZdService cdZdService;
    @Autowired
    YzGeoJsonDao yzGeoJsonDao;
    @Override
    public HandlerResult handler(AnalyseModel analyseModel, HttpResult httpResult) throws IOException {
        log.info("处理重叠宗地分析,状态码:{}",httpResult.getCode());
        HandlerResult result;
        if(HttpStatus.SC_OK==httpResult.getCode()){
            String newBody = XmlUtil.parseXml(httpResult.getBody());
            httpResult.setBody(newBody);
            /**保存占压信息**/
            AlyResult alyResult = objectMapper.readValue(httpResult.getBody(), AlyResult.class);

            List<AlyResultCdZd> alyResultCdZds = objectMapper.readValue(alyResult.getAnalysisResult().getResultRecords(),
                    new TypeReference<List<AlyResultCdZd>>() {
                    });
            /****如果不为空，存入数据库****/
            if(CollectionUtils.isNotEmpty(alyResultCdZds)) {
                alyResultCdZds.forEach(cdZd -> {
                    YzGeoJson yzGeoJson = new YzGeoJson();
                    String yzGeoJsonId = UUID.randomUUID().toString().replaceAll("-","");
                    yzGeoJson.setId(yzGeoJsonId);
                    yzGeoJson.setGeoJson(cdZd.getGeojson());
                    yzGeoJsonDao.saveYzGeoJson(yzGeoJson);
                    cdZd.setMongoId(yzGeoJsonId);
                    cdZd.setFxbs(analyseModel.getAlyId());
                });
                cdZdService.saveBatch(alyResultCdZds);
                return HandlerResult.createByErrorMessage("查询有"+alyResultCdZds.stream()+"个重叠宗地");
            }
            result = HandlerResult.createBySuccess(httpResult);
        }else{
            result = HandlerResult.createBySpatiaAnalyse_error("调用分析失败，请查看分析结果内容",httpResult);
        }
        return result;
    }
}
